package graphics;


import java.lang.Integer;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import graphics.PrintPreviewJPanel;

import java.lang.String;
import core.DynamicTableModel;
import core.Message;
import core.ProductManagement;
import core.UtilityFunctions;


import java.awt.Graphics2D;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.awt.Image;

import com.toedter.calendar.JDateChooser;

import db.DBConnection;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.ImageObserver;
import java.awt.print.Printable;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class OrdineJPanel extends javax.swing.JPanel implements Printable {
	
	private int numeroRigheTabella;
	
	private JLabel consegnaNjLabel;
	private JTextField numeroConsegnajTextField;
	private JLabel dataOrdinejLabel;
	private JButton aggiungiRigajButton;
	private JTextField prezzoAlSaccojTextField;
	private JTextField prezzoAlBancalejTextField;
	private JTextField descrizioneProdottojTextField;
	private JTextField numeroSacchijTextField;
	private JTextField numeroBancalijTextField;
	private JLabel prezzoAlSaccojLabel;
	private JLabel prezzoAlBancalejLabel;
	private JLabel discrizionejLabel;
	private JLabel numeroSacchijLabel;
	private JDateChooser DataConsegnaPrevistajDateChooser1;
	private JButton anteprimaStampajButton;
	private AbstractAction aggiungiRigaOrdineAbstractAction;
	private JButton stampajButton;
	private JButton salvaOrdinejButton;
	private JTable ordinejTable;
	private JPanel jPanelContainer1;
	private JPanel jPanelContainerTable;
	private JButton cercaProdottojButton;
	private JLabel numeroBancalijLabel;
	private JPanel jPanelContainerRiga;
	private JLabel identificazioneClientejLabel;
	private JButton SelezionaClientejButton;
	private JLabel ClientejLabel;
	private JPanel jPanelContainer2;
	private JLabel DataConsegnajLabel;
	private JLabel logojLabel;
	private JDateChooser DataOrdinejDateChooser;
	
	private VisualizzaClientiJPanel visualizzaClienti;
	private VisualizzaProdottiJPanel visualizzaProdotti;
	
	private int codiceCliente = -1;
	private int codiceProdotto = -1;
	
	private static int COLONNA_QUANTITA_BANCALI = 50;
	private static int RIGA_NUMERO_CONSEGNA = 164;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new OrdineJPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public OrdineJPanel() {
		super();
		this.numeroRigheTabella = 0;
		initGUI();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(818, 646));
			{
				jPanelContainer1 = new JPanel();
				GroupLayout jPanelContainer1Layout = new GroupLayout((JComponent)jPanelContainer1);
				jPanelContainer1.setLayout(jPanelContainer1Layout);
				jPanelContainer1.setBorder(BorderFactory.createTitledBorder(""));
				{
					consegnaNjLabel = new JLabel();
					consegnaNjLabel.setText("Consegna N.");
				}
				{
					numeroConsegnajTextField = new JTextField();
					numeroConsegnajTextField.addKeyListener(new KeyListener() {
						
						@Override
						public void keyTyped(KeyEvent arg0) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyReleased(KeyEvent arg0) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyPressed(KeyEvent arg0) {
							// TODO Auto-generated method stub
							performKeyListenerAction(numeroConsegnajTextField, arg0);
						}	
					});
				}
				{
					dataOrdinejLabel = new JLabel();
					dataOrdinejLabel.setText("Data Ordine");
				}
				{
					Calendar cal = Calendar.getInstance();
					Date data = new Date();
					data.setTime(cal.getTimeInMillis());
					System.out.println(data);
					DataOrdinejDateChooser = new JDateChooser(data);
					DataOrdinejDateChooser.addKeyListener(new KeyListener() {
						
						@Override
						public void keyTyped(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyReleased(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyPressed(KeyEvent e) {
							// TODO Auto-generated method stub
							performKeyListenerAction(null, e);
							if(e.getKeyCode() == KeyEvent.VK_ENTER)
							{
								DataOrdinejDateChooser.transferFocus();
							}
						}
					});
					
				}
				{
					DataConsegnajLabel = new JLabel();
					DataConsegnajLabel.setText("Data Consegna Prevista");
				}
				{
					DataConsegnaPrevistajDateChooser1 = new JDateChooser();
				}
					jPanelContainer1Layout.setHorizontalGroup(jPanelContainer1Layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(consegnaNjLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(numeroConsegnajTextField, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(dataOrdinejLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addComponent(DataOrdinejDateChooser, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(DataConsegnajLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(DataConsegnaPrevistajDateChooser1, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(18, Short.MAX_VALUE));
					jPanelContainer1Layout.setVerticalGroup(jPanelContainer1Layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(jPanelContainer1Layout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelContainer1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					        .addComponent(numeroConsegnajTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addComponent(consegnaNjLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)
					        .addComponent(dataOrdinejLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addComponent(DataConsegnajLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelContainer1Layout.createParallelGroup()
					        .addGroup(jPanelContainer1Layout.createSequentialGroup()
					            .addComponent(DataOrdinejDateChooser, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
					            .addGap(0, 0, Short.MAX_VALUE))
					        .addGroup(jPanelContainer1Layout.createSequentialGroup()
					            .addComponent(DataConsegnaPrevistajDateChooser1, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
					            .addGap(0, 0, Short.MAX_VALUE))))
					.addContainerGap());
			}
			{
				jPanelContainer2 = new JPanel();
				jPanelContainer2.setBorder(BorderFactory.createTitledBorder(""));
				{
					ClientejLabel = new JLabel();
					jPanelContainer2.add(ClientejLabel);
					ClientejLabel.setText("Cliente");
				}
				{
					SelezionaClientejButton = new JButton();
					jPanelContainer2.add(SelezionaClientejButton);
					SelezionaClientejButton.setText("Seleziona Cliente (F8)");
					SelezionaClientejButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/Add.png")));
					SelezionaClientejButton.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent e) {
							// TODO Auto-generated method stub
							JFrame frame = new JFrame();
							visualizzaClienti = new VisualizzaClientiJPanel(true);
							frame.getContentPane().add(visualizzaClienti);
							visualizzaClienti.setSelezionaClientiJButton();
							frame.setTitle("Seleziona Cliente | eOrder - Software per la gestione degli ordini sviluppato da Marco Cozza");
							visualizzaClienti.getVisualizzaSelezionajButton().setAction(getSelezionaClienteJButtonFromVisualizzaClientePanel(frame));
							frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
							frame.pack();
							frame.setVisible(true);
							frame.setLocationRelativeTo(null);
							visualizzaClienti.setButtonVisualizzaSelezionaIcon();
						}
					});
					SelezionaClientejButton.addKeyListener(new KeyListener() {
						
						@Override
						public void keyTyped(KeyEvent arg0) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyReleased(KeyEvent arg0) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyPressed(KeyEvent arg0) {
							// TODO Auto-generated method stub
							if(arg0.getKeyCode() == KeyEvent.VK_ENTER)
							{
								SelezionaClientejButton.transferFocus();
							}
							else
								performKeyListenerAction(null, arg0);
							
						}
					});
				}
				{
					identificazioneClientejLabel = new JLabel();
					jPanelContainer2.add(identificazioneClientejLabel);
				}
			}
			{
				jPanelContainerRiga = new JPanel();
				GridBagLayout jPanelContainerRigaLayout = new GridBagLayout();
				jPanelContainerRigaLayout.rowWeights = new double[] {0.1, 0.1, 0.1};
				jPanelContainerRigaLayout.rowHeights = new int[] {7, 7, 7};
				jPanelContainerRigaLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.1, 0.1, 0.1};
				jPanelContainerRigaLayout.columnWidths = new int[] {88, 87, 218, 7, 7, 7};
				jPanelContainerRiga.setLayout(jPanelContainerRigaLayout);
				jPanelContainerRiga.setBorder(BorderFactory.createTitledBorder(""));
				{
					numeroBancalijLabel = new JLabel();
					jPanelContainerRiga.add(numeroBancalijLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					numeroBancalijLabel.setText("#Bancali");
					numeroBancalijLabel.setPreferredSize(new java.awt.Dimension(54, 16));
				}
				{
					numeroSacchijLabel = new JLabel();
					jPanelContainerRiga.add(numeroSacchijLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					numeroSacchijLabel.setText("#Sacchi");
					numeroSacchijLabel.setPreferredSize(new java.awt.Dimension(47, 16));
				}
				{
					discrizionejLabel = new JLabel();
					jPanelContainerRiga.add(discrizionejLabel, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 4, 0, 0), 0, 0));
					discrizionejLabel.setText("Descrizione Prodotto");
					discrizionejLabel.setPreferredSize(new java.awt.Dimension(221, 16));
				}
				{
					prezzoAlBancalejLabel = new JLabel();
					jPanelContainerRiga.add(prezzoAlBancalejLabel, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					prezzoAlBancalejLabel.setText("Prezzo/Bancale");
					prezzoAlBancalejLabel.setPreferredSize(new java.awt.Dimension(88, 16));
				}
				{
					prezzoAlSaccojLabel = new JLabel();
					jPanelContainerRiga.add(prezzoAlSaccojLabel, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					prezzoAlSaccojLabel.setText("Prezzo/Sacco");
					prezzoAlSaccojLabel.setPreferredSize(new java.awt.Dimension(78, 16));
				}
				{
					numeroBancalijTextField = new JTextField();
					jPanelContainerRiga.add(numeroBancalijTextField, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					numeroBancalijTextField.setPreferredSize(new java.awt.Dimension(40, 28));
					numeroBancalijTextField.setColumns(5);
					numeroBancalijTextField.addKeyListener(new KeyListener() {
						
						@Override
						public void keyTyped(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyReleased(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyPressed(KeyEvent e) {
							// TODO Auto-generated method stub
							performKeyListenerAction(numeroBancalijTextField, e);
						}
					});
				}
				{
					numeroSacchijTextField = new JTextField();
					jPanelContainerRiga.add(numeroSacchijTextField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					numeroSacchijTextField.addKeyListener(new KeyListener() {
						
						@Override
						public void keyTyped(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyReleased(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyPressed(KeyEvent e) {
							// TODO Auto-generated method stub
							performKeyListenerAction(numeroSacchijTextField, e);
						}
					});
				}
				{
					descrizioneProdottojTextField = new JTextField();
					jPanelContainerRiga.add(descrizioneProdottojTextField, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					descrizioneProdottojTextField.addKeyListener(new KeyListener() {
						
						@Override
						public void keyTyped(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyReleased(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyPressed(KeyEvent e) {
							// TODO Auto-generated method stub
							performKeyListenerAction(descrizioneProdottojTextField, e);
						}
					});
				}
				{
					prezzoAlBancalejTextField = new JTextField();
					jPanelContainerRiga.add(prezzoAlBancalejTextField, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					prezzoAlBancalejTextField.addKeyListener(new KeyListener() {
						
						@Override
						public void keyTyped(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyReleased(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyPressed(KeyEvent e) {
							// TODO Auto-generated method stub
							performKeyListenerAction(prezzoAlBancalejTextField, e);
						}
					});
				}
				{
					prezzoAlSaccojTextField = new JTextField();
					jPanelContainerRiga.add(prezzoAlSaccojTextField, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					prezzoAlSaccojTextField.addKeyListener(new KeyListener() {
						
						@Override
						public void keyTyped(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyReleased(KeyEvent e) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void keyPressed(KeyEvent e) {
							// TODO Auto-generated method stub
							if(e.getKeyCode() == KeyEvent.VK_ENTER)
							{
								aggiungiRigaOrdine();
								SelezionaClientejButton.transferFocus();
								//DataOrdinejDateChooser.transferFocusBackward();
								//DataOrdinejDateChooser.transferFocusBackward();
								//DataOrdinejDateChooser.transferFocusBackward();
							}
							else
								performKeyListenerAction(prezzoAlSaccojTextField, e);
						}
					});
				}
				{
					aggiungiRigajButton = new JButton();
					jPanelContainerRiga.add(aggiungiRigajButton, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					aggiungiRigajButton.setText("Aggiungi");
					//aggiungiRigajButton.setAction(getAggiungiRigaOrdineAbstractAction());
					aggiungiRigajButton.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent arg0) {
							// TODO Auto-generated method stub
							aggiungiRigaOrdine();
						}
					});
				}
				{
					cercaProdottojButton = new JButton();
					jPanelContainerRiga.add(cercaProdottojButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					cercaProdottojButton.setText("Cerca Prodotto (F10)");
					cercaProdottojButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/Search_24x24.png")));
					cercaProdottojButton.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent arg0) {
							// TODO Auto-generated method stub
							initializeVisualizzaProdottiJPanel();
						}
					});
				}
			}
			{
				jPanelContainerTable = new JPanel();
				jPanelContainerTable.setBorder(BorderFactory.createTitledBorder(""));
				{
					ordinejTable = this.getOrdiniJTable();
					jPanelContainerTable.add(ordinejTable);
					ordinejTable.setPreferredSize(new java.awt.Dimension(644, 97));
					ordinejTable.setShowHorizontalLines(true);
					ordinejTable.setShowVerticalLines(true);
					ordinejTable.setRowHeight(18);
				}
			}
			{
				salvaOrdinejButton = new JButton();
				salvaOrdinejButton.setText("Salva Ordine (F2)");
				salvaOrdinejButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/save-icon32.png")));
				salvaOrdinejButton.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						salvaOrdine();
					}
				});
			}
			{
				stampajButton = new JButton();
				stampajButton.setText("Stampa Ordine (F12)");
				stampajButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/Print-icon.png")));
				stampajButton.setSize(172, 43);
				stampajButton.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						stampa();
					}
				});
			}
				thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(getLogojLabel(), GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
					.addComponent(jPanelContainer1, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addComponent(jPanelContainer2, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addComponent(jPanelContainerRiga, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addComponent(jPanelContainerTable, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(salvaOrdinejButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
					    .addComponent(stampajButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
					    .addComponent(getAnteprimaStampajButton(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(25, 25));
				thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addContainerGap(69, 69)
					.addGroup(thisLayout.createParallelGroup()
					    .addGroup(thisLayout.createSequentialGroup()
					        .addComponent(jPanelContainer1, GroupLayout.PREFERRED_SIZE, 682, GroupLayout.PREFERRED_SIZE))
					    .addGroup(thisLayout.createSequentialGroup()
					        .addComponent(jPanelContainer2, GroupLayout.PREFERRED_SIZE, 682, GroupLayout.PREFERRED_SIZE))
					    .addGroup(thisLayout.createSequentialGroup()
					        .addComponent(jPanelContainerRiga, GroupLayout.PREFERRED_SIZE, 682, GroupLayout.PREFERRED_SIZE))
					    .addGroup(thisLayout.createSequentialGroup()
					        .addComponent(jPanelContainerTable, GroupLayout.PREFERRED_SIZE, 682, GroupLayout.PREFERRED_SIZE))
					    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					        .addGroup(thisLayout.createParallelGroup()
					            .addComponent(getLogojLabel(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
					            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					                .addComponent(salvaOrdinejButton, GroupLayout.PREFERRED_SIZE, 172, GroupLayout.PREFERRED_SIZE)
					                .addGap(39)))
					        .addGap(31)
					        .addComponent(getAnteprimaStampajButton(), GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)
					        .addGap(55)
					        .addComponent(stampajButton, GroupLayout.PREFERRED_SIZE, 181, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(64, Short.MAX_VALUE));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JTable getOrdiniJTable()
	{
		if(ordinejTable == null)
		{
			String [] columnNames = { "Riga", "#Bancali", "#Sacchi", "Descrizione Prodotto", "Prezzo/Bancale", "Prezzo/Sacco" };
			Object[][] data = {
					{new Integer(1), "", "", "", "", ""},
					{new Integer(2), "", "", "", "", ""},
					{new Integer(3), "", "", "", "", ""},
					{new Integer(4), "", "", "", "", ""},
					{new Integer(5), "", "", "", "", ""}
			};
		
			DynamicTableModel ordinejTableModel = new DynamicTableModel(columnNames, data);
			ordinejTable = new JTable();
			ordinejTable.setModel(ordinejTableModel);
			ordinejTable.setVisible(true);
		}
		return ordinejTable;
	}
	
	public void stampa()
	{
		salvaOrdine();
		
		
		java.awt.print.PrinterJob pj = java.awt.print.PrinterJob.getPrinterJob();
		java.awt.print.PageFormat pg = new java.awt.print.PageFormat();
		pj.setJobName("Stampa Ordine");
		//pj.pageDialog(pg);
		pj.printDialog();
		//Printer printer = new Printer(this);
		pj.setPrintable(this);
		
		
		pg.setOrientation(java.awt.print.PageFormat.PORTRAIT);
		try {
			pj.print();
			//printer.print(this.getGraphics(), pg, 1);
			
		} catch (java.awt.print.PrinterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public int print(Graphics graphics, java.awt.print.PageFormat pageFormat, int pageIndex)
			throws java.awt.print.PrinterException {
		// TODO Auto-generated method stub
		if(pageIndex > 0)
			return NO_SUCH_PAGE;
		
		java.awt.Image image = null;
		try {
			image = ImageIO.read(getClass().getClassLoader().getResource("images/ricevuta.png"));
		} catch (java.io.IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.awt.Graphics2D graphics2d = (java.awt.Graphics2D)graphics;
		
		String numeroConsegna = numeroConsegnajTextField.getText();
		String dataOrdine = "";
		String dataConsegnaPrevista = "";
		
		if(DataOrdinejDateChooser.getCalendar() != null)
		{	
			int mese = DataOrdinejDateChooser.getCalendar().get(Calendar.MONTH) + 1;
			dataOrdine += DataOrdinejDateChooser.getCalendar().get(Calendar.DAY_OF_MONTH) + "/" + mese + "/" +DataOrdinejDateChooser.getCalendar().get(Calendar.YEAR);	
		}
		if(DataConsegnaPrevistajDateChooser1.getCalendar() != null)
		{	
			int mese = DataConsegnaPrevistajDateChooser1.getCalendar().get(Calendar.MONTH) + 1;
			dataConsegnaPrevista += DataConsegnaPrevistajDateChooser1.getCalendar().get(Calendar.DAY_OF_MONTH) + "/" + mese + "/" +DataConsegnaPrevistajDateChooser1.getCalendar().get(Calendar.YEAR);	
		}
		Font font = new Font("Arial", Font.PLAIN, 9);
		graphics2d.setFont(font);
		
		/**
		 * Pre-stampato ricevuta
		 */
		graphics2d.drawImage(image, (int)pageFormat.getImageableX()+10, (int)pageFormat.getImageableY()+50, (int)pageFormat.getWidth()-50, (int)pageFormat.getHeight()-100, null);
		//Stampa numero consegna
		graphics2d.drawString(numeroConsegna, (int)pageFormat.getImageableX()+110, (int)pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA);
		graphics2d.drawString(numeroConsegna, (int)pageFormat.getImageableX()+110, (int)(pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+pageFormat.getHeight()/2 - 37));
		//Stampa data ordine
		graphics2d.drawString(dataOrdine, (int)pageFormat.getImageableX()+235, (int)pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA);
		graphics2d.drawString(dataOrdine, (int)pageFormat.getImageableX()+235, (int)(pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+pageFormat.getHeight()/2 - 37));
		//Stampa data prevista consegna
		graphics2d.drawString(dataConsegnaPrevista, (int)pageFormat.getImageableX()+160, (int)pageFormat.getImageableY()+388);
		graphics2d.drawString(dataConsegnaPrevista, (int)pageFormat.getImageableX()+160, (int)(pageFormat.getImageableY()+388+pageFormat.getHeight()/2 -37));
		
		/** 
		 * Stampa dati cliente
		 */
		if(codiceCliente!=-1)
		{
			String query = "SELECT * FROM CLIENTE WHERE CodiceCliente =?;";
			UtilityFunctions ut = new UtilityFunctions();
			ResultSet res;
			try {
				res = ut.executeParametricQuerySingleParameter(new DBConnection(), query, ""+codiceCliente);
				res.next();
				
				if(res.getString("RagioneSociale").isEmpty())
				{
					graphics2d.drawString(res.getString("Nome").toUpperCase() + " " + res.getString("Cognome").toUpperCase(), (int)pageFormat.getImageableX()+312, (int)pageFormat.getImageableY()+78);
					graphics2d.drawString(res.getString("Nome").toUpperCase() + " " + res.getString("Cognome").toUpperCase(), (int)pageFormat.getImageableX()+312, (int)(pageFormat.getImageableY()+78+pageFormat.getHeight()/2 - 37));
				}
				else
				{
					graphics2d.drawString(res.getString("RagioneSociale") + " - " + res.getString("Nome").toUpperCase() + " " + res.getString("Cognome").toUpperCase(), (int)pageFormat.getImageableX()+312, (int)pageFormat.getImageableY()+78);
					graphics2d.drawString(res.getString("RagioneSociale") + " - " + res.getString("Nome").toUpperCase() + " " + res.getString("Cognome").toUpperCase(), (int)pageFormat.getImageableX()+312, (int)(pageFormat.getImageableY()+78+pageFormat.getHeight()/2 - 37));
				}
				
				graphics2d.drawString(res.getString("Indirizzo") + " - " + res.getString("Comune") + " ("+  res.getString("Provincia") + ")", (int)pageFormat.getImageableX()+312, (int)pageFormat.getImageableY()+98);
				graphics2d.drawString(res.getString("Indirizzo") + " - " + res.getString("Comune") + " ("+  res.getString("Provincia") + ")", (int)pageFormat.getImageableX()+312, (int)(pageFormat.getImageableY()+98+pageFormat.getHeight()/2 - 37));
				
				graphics2d.drawString(res.getString("NumeroTelefono"), (int)pageFormat.getImageableX()+312, (int)pageFormat.getImageableY()+118);
				graphics2d.drawString(res.getString("NumeroTelefono"), (int)pageFormat.getImageableX()+312, (int)(pageFormat.getImageableY()+118+pageFormat.getHeight()/2 - 37));
				if(res.getString("PartitaIva").isEmpty())
				{
					graphics2d.drawString(res.getString("CodiceFiscale"), (int)pageFormat.getImageableX()+342, (int)pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA);
					graphics2d.drawString(res.getString("CodiceFiscale"), (int)pageFormat.getImageableX()+342, (int)(pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+pageFormat.getHeight()/2 - 37));
				}
				else
				{
					graphics2d.drawString(res.getString("PartitaIva"), (int)pageFormat.getImageableX()+342, (int)pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA);
					graphics2d.drawString(res.getString("PartitaIva"), (int)pageFormat.getImageableX()+342, (int)(pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+pageFormat.getHeight()/2 - 37));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//graphics2d.drawImage(image, (int)pageFormat.getImageableX()+10, (int)pageFormat.getImageableY()+10+(int)pageFormat.getHeight()/2, 240, 153, null);
		
		/**
		 * Stampa righe ordine
		 */
		int spaziaturaRighe = 20;
		int sommaSpaziature = 0;
		
		for(int i=0; i<numeroRigheTabella; i++)
		{
			graphics2d.drawString(ordinejTable.getValueAt(i, 1).toString(), (int)pageFormat.getImageableX()+60, (int)pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+50+sommaSpaziature);
			graphics2d.drawString(ordinejTable.getValueAt(i, 1).toString(), (int)pageFormat.getImageableX()+60, (int)(pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+13+sommaSpaziature+pageFormat.getHeight()/2));
			
			graphics2d.drawString(ordinejTable.getValueAt(i, 2).toString(), (int)pageFormat.getImageableX()+110, (int)pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+50+sommaSpaziature);
			graphics2d.drawString(ordinejTable.getValueAt(i, 2).toString(), (int)pageFormat.getImageableX()+110, (int)(pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+13+sommaSpaziature+pageFormat.getHeight()/2));
			
			graphics2d.drawString(ordinejTable.getValueAt(i, 3).toString(), (int)pageFormat.getImageableX()+240, (int)pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+50+sommaSpaziature);
			graphics2d.drawString(ordinejTable.getValueAt(i, 3).toString(), (int)pageFormat.getImageableX()+240, (int)(pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+13+sommaSpaziature+pageFormat.getHeight()/2));
			
			String prezzoBancale = "";
			String prezzoSacco = "";
			
			
			
			if(ordinejTable.getValueAt(i, 4).toString().contains(".") || ordinejTable.getValueAt(i, 4).toString().contains(","))
			{
				String prezzoAlBancaleFormatted = ordinejTable.getValueAt(i, 4).toString().replace(".", ",");
				String prezzoAlBancale [];
				/**
				if(ordinejTable.getValueAt(i, 4).toString().contains("."))
					prezzoAlBancale = ordinejTable.getValueAt(i, 4).toString().split(".");
				else
					prezzoAlBancale = ordinejTable.getValueAt(i, 4).toString().split(",");
				**/
				prezzoAlBancale = prezzoAlBancaleFormatted.split(",");
				if(prezzoAlBancale[1].length() == 2)
					prezzoBancale += prezzoAlBancale[0] + "," + prezzoAlBancale[1];
				else
					prezzoBancale += prezzoAlBancale[0] + "," + prezzoAlBancale[1] + "0";
			}
			else
			{
				prezzoBancale += ordinejTable.getValueAt(i, 4).toString() + ",00";
			}
			
			graphics2d.drawString(prezzoBancale, (int)pageFormat.getImageableX()+430, (int)pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+50+sommaSpaziature);
			graphics2d.drawString(prezzoBancale, (int)pageFormat.getImageableX()+430, (int)(pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+13+sommaSpaziature+pageFormat.getHeight()/2));
			
			//ANCHE PER ALTRO PREZZO
			
			if(ordinejTable.getValueAt(i, 5).toString().contains(".") || ordinejTable.getValueAt(i, 5).toString().contains(","))
			{
				String prezzoAlSacco [];
				/**
				 
				if(ordinejTable.getValueAt(i, 5).toString().contains("."))
					prezzoAlSacco = ordinejTable.getValueAt(i, 5).toString().split(".");
				else
					prezzoAlSacco = ordinejTable.getValueAt(i, 5).toString().split(",");
				*/
				
				String prezzoAlSaccoFormatted = ordinejTable.getValueAt(i, 5).toString().replace(".", ",");
				prezzoAlSacco = prezzoAlSaccoFormatted.split(",");
				if(prezzoAlSacco[1].length() == 2)
					prezzoSacco += prezzoAlSacco[0] + "," + prezzoAlSacco[1];
				else
					prezzoSacco += prezzoAlSacco[0] + "," + prezzoAlSacco[1] + "0";
			}
			else
			{
				prezzoSacco += ordinejTable.getValueAt(i, 5).toString() + ",00";
			}
			
			graphics2d.drawString(prezzoSacco, (int)pageFormat.getImageableX()+488, (int)pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+50+sommaSpaziature);
			graphics2d.drawString(prezzoSacco, (int)pageFormat.getImageableX()+488, (int)(pageFormat.getImageableY()+this.RIGA_NUMERO_CONSEGNA+13+sommaSpaziature+pageFormat.getHeight()/2));
			
			sommaSpaziature += spaziaturaRighe;
		}
		
		
		return 0;
	}
	
	private void aggiungiRigaOrdine()
	{
		if(numeroRigheTabella < 5)
		{
			ordinejTable.setValueAt(this.numeroBancalijTextField.getText(), numeroRigheTabella, 1);
			ordinejTable.setValueAt(numeroSacchijTextField.getText(), numeroRigheTabella, 2);
			ordinejTable.setValueAt(this.descrizioneProdottojTextField.getText(), numeroRigheTabella, 3);
			ordinejTable.setValueAt(this.prezzoAlBancalejTextField.getText(), numeroRigheTabella, 4);
			ordinejTable.setValueAt(this.prezzoAlSaccojTextField.getText(), numeroRigheTabella, 5);
			this.numeroRigheTabella++;
			numeroBancalijTextField.setText("");
			numeroSacchijTextField.setText("");
			descrizioneProdottojTextField.setText("");
			prezzoAlBancalejTextField.setText("");
			prezzoAlSaccojTextField.setText("");
		}
}
	
	private AbstractAction getAggiungiRigaOrdineAbstractAction() {
		if(aggiungiRigaOrdineAbstractAction == null) {
			aggiungiRigaOrdineAbstractAction = new AbstractAction("Aggiungi", null) {
				public void actionPerformed(ActionEvent evt) {
					aggiungiRigaOrdine();
				}
			};
		}
		return aggiungiRigaOrdineAbstractAction;
	}

	public void anteprimaStampa()
	{
		JFrame j = new JFrame();
		j.setSize(700, 600);
		j.setVisible(true);
		PrintPreviewJPanel anteprima = new PrintPreviewJPanel(this);
		javax.swing.JScrollPane jsp = new javax.swing.JScrollPane(anteprima);
		j.setTitle("Anteprima di stampa fattura");
		
		//j.setContentPane(jsp);
		j.setTitle("Anteprima di stampa | eOrder - Software per la gestione degli ordini sviluppato da Marco Cozza");
		jsp.setViewportView(anteprima);
		jsp.setVerticalScrollBar(new javax.swing.JScrollBar());
		jsp.setSize(700, 600);
		jsp.revalidate();
		j.getContentPane().add(jsp);
		j.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			
	}
	
	/**
	 * @return the jButtonPrint
	 */
	public JButton copyjButtonPrint() {
		JButton print = new JButton();
		print.setText("Stampa");
		print.setToolTipText("Stampa ordine");
		print.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/Print-icon.png")));
		print.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				stampa();
			}
		});
		return print;
	}
	
	private JButton getAnteprimaStampajButton() {
		if(anteprimaStampajButton == null) {
			anteprimaStampajButton = new JButton();
			anteprimaStampajButton.setText("Anteprima di Stampa (F5)");
			anteprimaStampajButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/Preview-icon.png")));
			anteprimaStampajButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					anteprimaStampa();
				}
			});
		}
		return anteprimaStampajButton;
	}
	
	private AbstractAction getSelezionaClienteJButtonFromVisualizzaClientePanel(final JFrame frame)
	{
		if(visualizzaClienti.buttonSelectAbstractAction == null)
		{
			visualizzaClienti.buttonSelectAbstractAction = new AbstractAction("Seleziona (F12)", null) {
			public void actionPerformed(ActionEvent evt) {
				int temp = visualizzaClienti.getClientijTable().getSelectedRow();
				if(temp != -1) 
				{
					//temp++;
					codiceCliente = Integer.parseInt(visualizzaClienti.getClientijTable().getValueAt(temp, 0).toString());
					//visualizzaClienti.dispose();
					System.out.println("OKOK");
					setJLabelCliente();
					SelezionaClientejButton.transferFocus();
					frame.dispose();
				}
			}
		};
		visualizzaClienti.getVisualizzaSelezionajButton().setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/Select.png")));
		}
		return visualizzaClienti.buttonSelectAbstractAction;
	}
	
	private AbstractAction getSelezionaProdottoJButtonFromVisualizzaProdottoPanel(final JFrame frame)
	{
		if(visualizzaProdotti.buttonSelectAbstractAction == null)
		{
			visualizzaProdotti.buttonSelectAbstractAction = new AbstractAction("Seleziona (F12)", null) {
				
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int temp = visualizzaProdotti.getProdottijTable().getSelectedRow();
				if(temp != -1) 
				{
					//temp++;
					codiceProdotto = Integer.parseInt(visualizzaProdotti.getProdottijTable().getValueAt(temp, 0).toString());
					//visualizzaProdotti.dispose();
					setJTextFieldProdotto();
					descrizioneProdottojTextField.transferFocus();
					frame.dispose();
				}
			}
			};
			visualizzaProdotti.getVisualizzaSelezionajButton().setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/view.png")));
		}
		return visualizzaProdotti.buttonSelectAbstractAction;
	}
	
	private void setJLabelCliente()
	{
		if(this.codiceCliente!=-1)
		{
			String query = "SELECT * FROM CLIENTE WHERE CodiceCliente =?;";
			UtilityFunctions ut = new UtilityFunctions();
			try {
				ResultSet res = ut.executeParametricQuerySingleParameter(new DBConnection(), query, ""+codiceCliente);
				res.next();
				String testoJLabel = ""+ codiceCliente + " - " + res.getString("RagioneSociale") +
					" - " + res.getString("Cognome") + " " + res.getString("Nome");
				identificazioneClientejLabel.setText(testoJLabel);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private void setJTextFieldProdotto()
	{
		if(this.codiceProdotto!=-1)
		{
			String query = "SELECT * FROM PRODOTTO WHERE CodiceProdotto =?;";
			UtilityFunctions ut = new UtilityFunctions();
			try {
				ResultSet res = ut.executeParametricQuerySingleParameter(new DBConnection(), query, ""+codiceProdotto);
				res.next();
				String testoJLabel = ""+ codiceProdotto + " - " + res.getString("NomeProdotto");
				descrizioneProdottojTextField.setText(testoJLabel);
				//anche prezzi prodotti??
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private void performKeyListenerAction(JTextField jtext, KeyEvent key)
	{
		int tasto = key.getKeyCode();
		if(tasto == KeyEvent.VK_F12)
		{
			stampa();
		}
		else if(tasto == KeyEvent.VK_F8)
		{
			JFrame frame = new JFrame();
			visualizzaClienti = new VisualizzaClientiJPanel(true);
			frame.getContentPane().add(visualizzaClienti);
			frame.setTitle("Seleziona Cliente | eOrder - Software per la gestione degli ordini sviluppato da Marco Cozza");
			visualizzaClienti.setSelezionaClientiJButton();
			visualizzaClienti.getVisualizzaSelezionajButton().setAction(getSelezionaClienteJButtonFromVisualizzaClientePanel(frame));
			frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			frame.pack();
			frame.setVisible(true);
			frame.setLocationRelativeTo(null);
			visualizzaClienti.setButtonVisualizzaSelezionaIcon();
		}
		else if(tasto == KeyEvent.VK_F2)
		{
			salvaOrdine();
		}
		else if(tasto == KeyEvent.VK_F5)
		{
			anteprimaStampa();
		}
		else if(tasto == KeyEvent.VK_F10)
		{
			initializeVisualizzaProdottiJPanel();
		}
		else if(tasto == KeyEvent.VK_ENTER)
		{
			jtext.transferFocus();
		}
	}
	
	private void initializeVisualizzaProdottiJPanel()
	{
		JFrame frame = new JFrame();
		visualizzaProdotti = new VisualizzaProdottiJPanel(true);
		frame.getContentPane().add(visualizzaProdotti);
		visualizzaProdotti.setSelezionaProdottiJButton();
		visualizzaProdotti.getVisualizzaSelezionajButton().setAction(getSelezionaProdottoJButtonFromVisualizzaProdottoPanel(frame));
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setTitle("Seleziona Prodotto | eOrder - Software per la gestione degli ordini sviluppato da Marco Cozza");
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		visualizzaProdotti.setButtonVisualizzaSelezionaIcon();
	}
	
	private void salvaOrdine()
	{
		if(numeroConsegnajTextField.getText().isEmpty() || DataOrdinejDateChooser.getCalendar() == null || codiceCliente == -1)
		{
			Message mex = new Message(new JFrame());
			mex.customError("Impossibile salvare l'ordine, mancano alcuni dati", "Errore salvataggio ordine");
		}
		else if(numeroRigheTabella == 0)
		{
			Message mex = new Message(new JFrame());
			mex.customError("Impossibile salvare l'ordine, manca il contenuto", "Errore salvataggio ordine");
		}
		else
		{
			String querySalvaOrdine = "INSERT INTO ORDINE(NumeroConsegna, DataOrdine, DataConsegnaPrevista, Cliente)" +
									" VALUES(?, ?, ?, ?);";
			String dataOrdine = "";
			int mese = DataOrdinejDateChooser.getCalendar().get(Calendar.MONTH) + 1;
			dataOrdine += DataOrdinejDateChooser.getCalendar().get(Calendar.YEAR) + "-" + mese + "-" + DataOrdinejDateChooser.getCalendar().get(Calendar.DAY_OF_MONTH);
			String dataConsegnaPrevista = "";
			mese = DataConsegnaPrevistajDateChooser1.getCalendar().get(Calendar.MONTH) + 1;
			dataConsegnaPrevista += DataConsegnaPrevistajDateChooser1.getCalendar().get(Calendar.YEAR) + "-" + mese + "-" + DataConsegnaPrevistajDateChooser1.getCalendar().get(Calendar.DAY_OF_MONTH);
			String parametriOrdine [] = {numeroConsegnajTextField.getText(), dataOrdine, dataConsegnaPrevista, codiceCliente+""};
			
			Message mex = new Message(new JFrame());
			UtilityFunctions ut = new UtilityFunctions();
			
			String queryCodiceOrdine = "SELECT CodiceOrdine FROM ORDINE WHERE NumeroConsegna = ? AND DataOrdine = ? AND DataConsegnaPrevista = ? AND Cliente  = ?;";
			String codiceOrdine;
			
			try {
				DBConnection dbCon = new DBConnection();
				ut.executeParametricUpdate(dbCon, querySalvaOrdine, parametriOrdine);
				ResultSet res = ut.executeParametricQuery(dbCon, queryCodiceOrdine, parametriOrdine);
				res.next();
				codiceOrdine = res.getString(1);
				
				String querySalvaContenuto = "INSERT INTO RIGAORDINE (QuantitaBancali, QuantitaSacchi, PrezzoAlBancale, PrezzoAlSacco, CodiceOrdine, CodiceProdotto) " +
											" VALUES(?, ?, ?, ?, ?, ?);";
				
				String parametriContenuto [] = new String[6];
				
				for(int i=0; i<numeroRigheTabella; i++)
				{
					parametriContenuto[0] = ordinejTable.getValueAt(i, 1).toString();
					parametriContenuto[1] = ordinejTable.getValueAt(i, 2).toString();
					parametriContenuto[2] = ordinejTable.getValueAt(i, 4).toString().replace(",", ".");
					parametriContenuto[3] = ordinejTable.getValueAt(i, 5).toString().replace(",", ".");
					parametriContenuto[4] = codiceOrdine;
					String[] splittingProdotto = ordinejTable.getValueAt(i, 3).toString().split("-");
					parametriContenuto[5] = splittingProdotto[0];
					
					ut.executeParametricUpdate(dbCon, querySalvaContenuto, parametriContenuto);
				}
				
				mex.info("Salvataggio ordine effettuato correttamente");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				mex.customError("Impossibile salvare l'ordine, errore imprevisto", "Errore salvataggio ordine");
				
			}
			
		}
		
	}
	
	private JLabel getLogojLabel() {
		if(logojLabel == null) {
			logojLabel = new JLabel();
			logojLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/shopping.png")));
		}
		return logojLabel;
	}
}
